/*
 * Created on Feb 16, 2004
 *
 * Author: Sandeep Prakash
 * Project: Sucxentv2 (using leaf nodes only)
 */
package dbsucxentW.storageEdgeW.parserW;

import java.io.BufferedWriter;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;


/**
 * @author Administrator
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class NodeExtractor extends DefaultHandler
{
	protected int _order = 0;
	protected int _depth = 0;
	
	protected StringBuilder _path;
	protected StringBuilder _leafValue;
	protected int _ancestorId[];
	
	//extend BS
	protected String[] _elementName;
	//klarinda 20060823 - Element is not used, protected Element _ordinalManager;

	protected boolean started = false;	// klarinda 20060823 for empty element nodes
	
	private int counter = 0;
	
	//protected DescendantManager _descendant;			//klarinda 20061005 (clean up)
	
	//bulk loader (klarinda 20060905)
//	protected final static int NO_OF_TABLES 	= 4;	//attribute, doc, edge path
//	protected final static int DOCUMENT_TABLE 	= 0;	
//	protected final static int PATH_TABLE 		= 1;	
//	protected final static int EDGE_TABLE 		= 2;	
//	protected final static int ATTRIBUTE_TABLE 	= 3;	
//	//protected final static String BULKFILEPATH = "D:/sucxent/sucxent/bulk/Edge/";
//	protected final static String BULKFILEPATH = "C:/Klarinda/sucxent/bulk/Edge/"; //for experiments
//	protected final static String[] TABLE_NAMES = {"Document", "Path", "Edge", "Attribute"};
	
	protected BufferedWriter _writers[];
	protected int _docId = 1; //hardcode the docId first for experiment purpose
	//end of bulk loader
	
	public NodeExtractor(){
		
		_leafValue = new StringBuilder();
		_path = new StringBuilder();
		_elementName = new String[255];
		_ancestorId = new int[255];
		//klarinda 20060823 - Element is not used, _ordinalManager = new Element();
		//_descendant = new DescendantManager();	//klarinda 20061005 (clean up)
	}
	
	public void endDocument() throws SAXException
	{
		//Launcher.getLoggerInstance().info( "Completed extraction");
	}

	/* (non-Javadoc)
	 * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
	 */
	public void endElement(String uri, String localName, String qName) throws SAXException
	{
//		System.out.println( "NodeExtractor.endElement("+uri+","+localName+","+qName+")");//klarinda 20060822
		//
		//Call leaf() only if leaf was encountered
		//

		if( _leafValue.toString().trim().length() > 0 || started){ //klarinda 20060823 add started
			leaf();
		}
		//extend XParent
		else{
			nonLeaf();
		}
		started = false;		//klarinda 20060823
		
		_depth--;
		clearPath();
		
	}

	/* (non-Javadoc)
	 * @see org.xml.sax.ContentHandler#startDocument()
	 */
	public void startDocument() throws SAXException{
		//Launcher.getLoggerInstance().info("start loh");
	}

	/* (non-Javadoc)
	 * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
	 */
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
	{
//		System.out.println( "NodeExtractor.startElement("+uri+","+localName+","+qName+","+attributes+")");//klarinda 20060822
		
		started = true;		//klarinda 20060823

		_leafValue = new StringBuilder();
		_path.append( "." );
		_path.append( qName );
		_path.append( "#" );
		_depth++;
//		attributes(attributes);					
//		 extend BS
		_order++;
		
		_elementName[_depth] = qName; 
		_ancestorId[_depth] = _order;
		//Launcher.getLoggerInstance().info( "Element: " + _path.toString());
		
//		System.out.println( "NodeExtractor.startElement(), path       : " + _path);//klarinda 20060822
//		System.out.println( "NodeExtractor.startElement(), depth      : " + _depth);//klarinda 20060822
//		System.out.println( "NodeExtractor.startElement(), order      : " + _order);//klarinda 20060822
//		System.out.println( "NodeExtractor.startElement(), elementName: " + _elementName[_depth]);//klarinda 20060822
		
		attributes(attributes);
		
	}

	protected void attributes(Attributes attributes) {
//		System.out.println( "NodeExtractor.attributes()");	//klarinda 20060822
		/*klarinda 20060828 is this class being called? Coz it's been overridden by BranchOrderDecorator

		int attrCount = attributes.getLength();
		
		for( int i = 0; i < attrCount; i++ ){
			_path.append( ".@" );
			_path.append( attributes.getQName( i ) );
			_path.append( "#" );
			
			_leafValue = new StringBuilder( attributes.getValue( i ) );
			_depth++;
			
			_order++;

			System.out.println( "NodeExtractor.attributes(), path     : " + _path);//klarinda 20060822
			System.out.println( "NodeExtractor.attributes(), depth    : " + _depth);//klarinda 20060822
			System.out.println( "NodeExtractor.attributes(), order    : " + _order);//klarinda 20060822
			System.out.println( "NodeExtractor.attributes(), leafValue: " + _leafValue);//klarinda 20060822

			leaf();
			
			_depth--;
			clearPath();
			
		}
		*/
	
	}

	/* (non-Javadoc)
	 * @see org.xml.sax.ContentHandler#characters(char[], int, int)
	 */
	public void characters(char[] ch, int start, int length) throws SAXException
	{
//		System.out.println( "NodeExtractor.characters()");//klarinda 20060822
		//
		//Recognize character data..
		//Ignore all chunks that do not start with a proper character
		
		if( length > 0 ){
			_leafValue.append( ch, start, length );
		}
//		System.out.println( "NodeExtractor.characters(), _leafValue: " + _leafValue);//klarinda 20060822
	}

	/**
	 * Method is called to clear _path till the last startElement
	 *
	 */
	protected void clearPath(){
//		System.out.println( "NodeExtractor.clearPath()");//klarinda 20060822
		int index = _path.lastIndexOf( "." );
		_path.delete( index, Integer.MAX_VALUE );
		
		_leafValue = new StringBuilder();
	}
	
	/**
	 * This method is called whenever a leaf node : attribute or element is seen.
	 *
	 */
	protected void leaf(){
	}
	
	protected void nonLeaf(){
	}
	
	//klarinda 20060828
	protected void leafAttribute(){
	}

}
